﻿using System;
using Coolite.Ext.Web;
using Pap.Web.Modules.Loan.ViewModels;
using Pap.Web.Utils;
using System.Collections.Generic;
using Pap.Services;
using Newtonsoft.Json.Linq;
using System.Linq;
using Pap.Data.Abstraction.Utils;
using Microsoft.Practices.ServiceLocation;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FreezLoans : BaseUserControl<FreezLoansViewModel>
    {
        /// <summary>
        /// 
        /// </summary>
        
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillFrozenLoans();
            }
        }

        /// <summary>
        /// Fills the frozen loans.
        /// </summary>
        private void FillFrozenLoans()
        {
            this.storeFozenLoans.DataSource = this.ViewModel.GetFrozenLoans();
            this.storeFozenLoans.DataBind();
        }
        /// <summary>
        /// Handles the Clicked event of the BtnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnSubmit_Clicked(object sender, AjaxEventArgs e)
        {
            //this.gvFrozenLoans.SelectionModel[0]
            //this.ViewModel.Unfreez();

            FillFrozenLoans();
        }

        protected void BtnMoveLoan_Clicked(object sender, AjaxEventArgs e)
        {
            using (IUnitOfWork trancaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                RowSelectionModel selectionModel = this.gvFrozenLoans.SelectionModel.Primary as RowSelectionModel;

                foreach (var row in selectionModel.SelectedRows)
                {
                    this.ViewModel.UnfreezLoan(row.RecordID.ToGuid());
                }
                trancaction.Commit();
                // refresh the list
                FillFrozenLoans();
            }



        }
    }
}